package gui;

import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JLabel;

public class SimpleHyperlink extends JLabel implements MouseListener {

	private URI uri;
	private boolean isValid;

	/**
	 * constructor which produces a valid SimpleHyperlink object
	 * 
	 * @param url
	 * @param text
	 */
	SimpleHyperlink(String url, String text) {
		try {
			this.setUri(new URI(url));
			this.setText("<html><body><u>" + text + "</u></body></html>");
			this.addMouseListener(this);
			// this.setFont(getFont().deriveFont(Font.ITALIC));
			this.setFont();
			this.isValid = true;
		} catch (URISyntaxException ex) {
			System.err.println("Fehler: URI konnte nicht erzeugt werden");
		}
	}

	private void setFont() {
		Font font = getFont();
		font = font.deriveFont(Font.ITALIC);
		super.setFont(font);
	}

	public void mouseClicked(MouseEvent e) {
		try {
			Desktop.getDesktop().browse(this.getUri());
		} catch (IOException e1) {
			System.err.println("Fehler: URI konnte nicht aufgerufen werden");
			e1.printStackTrace();
		}
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * @return the isValid
	 */
	public boolean isValid() {
		return this.isValid;
	}

	/**
	 * @param uri
	 *           the uri to set
	 */
	public void setUri(URI uri) {
		this.uri = uri;
	}

	/**
	 * @return the uri
	 */
	public URI getUri() {
		return this.uri;
	}

}
